//
//  RadioCollectionViewController.m
//  OurRelaxDemo
//
//  Created by 于宾尧 on 15/6/25.
//  Copyright (c) 2015年 于宾尧. All rights reserved.
//

#import "RadioCollectionViewController.h"

@interface RadioCollectionViewController ()

@end

@implementation RadioCollectionViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self subMyViews];
        self.playUrlArray = [NSMutableArray array];
        
    }
    return self;
}
-(void)subMyViews
{

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(125, 125);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    flowLayout.minimumInteritemSpacing = 10.0;

        self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , 150) collectionViewLayout:flowLayout];
        self.myCollectionView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:self.myCollectionView];
    
        self.myCollectionView.delegate = self;
        self.myCollectionView.dataSource = self;
        
        [self.myCollectionView registerClass:[bigRadioCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        
        
        
        
    }
    
    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    {
        return 1 ;
    }
    
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        
       
        return self.classicArray.count ;
    }
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        self.cell.bigRadioModels = self.classicArray[indexPath.row];

        return self.cell;
        
        
        
    
    }
-(void)getRadioPlayingUrl :(bigRadioModels *)radio

{

    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/30?device=iPhone",radio.albumId ];
    
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *infoDic = [dic objectForKey:@"tracks"];
        NSMutableArray *array = [infoDic objectForKey:@"list"];
        for (NSMutableDictionary *urlDic in array) {
            UrlModel *urlMdl = [[UrlModel alloc]init];
            [urlMdl setValuesForKeysWithDictionary:urlDic];
            NSString *playUrl = urlMdl.playUrl64;
            [self.playUrlArray addObject:playUrl];
            
        }
        [Player Play:self.playUrlArray];
        [Player shareDataHandle].playingIndex = 0;
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.myDelegate pushToWords11];
    [Player shareDataHandle].playingIndex = 0;

    [self.playUrlArray removeAllObjects];
    [[Player shareDataHandle].playUrlArray removeAllObjects];
    bigRadioModels *radio = [self.classicArray objectAtIndex:indexPath.row];
    [self getRadioPlayingUrl:radio];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
